package com.alibaba.citrus.ots.application.report.ability.repository.impl;

import com.alibaba.citrus.ots.application.report.ability.repository.OrderProgressMonitorReportQueryRepository;
import com.alibaba.cz.base.tool.Nullable;
import com.alibaba.cz.base.tool.page.PageResult;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.otsapplicationreportsdk.contants.OtsApplicationReportSdkConstants;
import com.epoch.app.otsapplicationreportsdk.model.dto.OrderProgressMonitorReportLoadListVO;
import com.epoch.app.otsapplicationreportsdk.model.dto.OrderProgressMonitorReportSDO;
import com.epoch.app.otsapplicationreportsdk.model.dto.QueryOrderMonitorReportRequest;
import com.epoch.app.otsapplicationreportsdk.orderprogressmonitorreport.model.OrderProgressMonitorReport;
import com.epoch.app.otsapplicationreportsdk.orderprogressmonitorreport.service.OrderProgressMonitorReportService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhoubc
 * @description: TODO
 * @date 2022/1/6 20:39
 */
@Component
public class OrderProgressMonitorReportQueryRepositoryImpl implements OrderProgressMonitorReportQueryRepository {

    @Resource
    OrderProgressMonitorReportService orderProgressMonitorReportService;

    @Override
    public PageResult<List<OrderProgressMonitorReportSDO>> pageQueryOrderMonitorReportList(QueryOrderMonitorReportRequest request) {
        OrderProgressMonitorReportLoadListVO listVO = OrderProgressMonitorReportLoadListVO.newOrderProgressMonitorReportLoadListVOBuilder()
                        .parentOrganizationId(request.getParentOrganizationId())
                        .organizationIds(request.getOrganizationIds())
                        .bizType(request.getBizType())
                        .statisticalDate(request.getStatisticalDate())
                        .beginDate(request.getBeginDate())
                        .endDate(request.getEndDate())
                        .notInOrganizationIds(request.getFilterOrganizationIds())
                        .build();
        listVO.setStart(request.getStart());
        listVO.setLimit(request.getLimit());
        Result<List<OrderProgressMonitorReport>> list = orderProgressMonitorReportService.loadListForPage(listVO);
        List<OrderProgressMonitorReportSDO> result = Nullable.stream(list.getResult()).map(x -> {
            OrderProgressMonitorReportSDO bean = new OrderProgressMonitorReportSDO();
            BeanUtils.copyProperties(x,bean);
            return bean;
        }).collect(Collectors.toList());
        return PageResult.of(result,list.getTotal());
    }

    @Override
    public List<OrderProgressMonitorReportSDO> getOrderMonitorReportList(QueryOrderMonitorReportRequest request) {
        Result<List<OrderProgressMonitorReport>> list = null;
        OrderProgressMonitorReportLoadListVO listVO = OrderProgressMonitorReportLoadListVO.newOrderProgressMonitorReportLoadListVOBuilder()
                .organizationId(request.getOrganizationId())
                .bizType(request.getBizType())
                .statisticalDate(request.getStatisticalDate())
                .build();
        listVO.setStart(request.getStart());
        listVO.setLimit(request.getLimit());
        if("1".equals(OtsApplicationReportSdkConstants.QUERY_READONLY_DATABASE())){
            list = orderProgressMonitorReportService.getloadListReadOnly(listVO);
        }else{
            list = orderProgressMonitorReportService.getloadList(listVO);
        }
        List<OrderProgressMonitorReportSDO> result = Nullable.stream(list.getResult()).map(x -> {
            OrderProgressMonitorReportSDO bean = new OrderProgressMonitorReportSDO();
            BeanUtils.copyProperties(x,bean);
            return bean;
        }).collect(Collectors.toList());
        return result;
    }

    @Override
    public List<OrderProgressMonitorReportSDO> statisticalQuery(QueryOrderMonitorReportRequest request) {
        OrderProgressMonitorReportLoadListVO listVO = OrderProgressMonitorReportLoadListVO.newOrderProgressMonitorReportLoadListVOBuilder()
                .parentOrganizationId(request.getParentOrganizationId())
                .organizationIds(request.getOrganizationIds())
                .bizType(request.getBizType())
                .statisticalDate(request.getStatisticalDate())
                .beginDate(request.getBeginDate())
                .endDate(request.getEndDate())
                .notInOrganizationIds(request.getFilterOrganizationIds())
                .build();
        listVO.setStart(0);
        listVO.setLimit(1000);
        Result<List<OrderProgressMonitorReport>> list = orderProgressMonitorReportService.loadListForPage(listVO);
        List<OrderProgressMonitorReportSDO> result = Nullable.stream(list.getResult()).map(x -> {
            OrderProgressMonitorReportSDO bean = new OrderProgressMonitorReportSDO();
            BeanUtils.copyProperties(x,bean);
            return bean;
        }).collect(Collectors.toList());
//        OrderProgressMonitorReportLoadListVO listVO = OrderProgressMonitorReportLoadListVO.newOrderProgressMonitorReportLoadListVOBuilder()
//                .parentOrganizationId(request.getParentOrganizationId())
//                .organizationIds(request.getOrganizationIds())
//                .bizType(request.getBizType())
//                .statisticalDate(request.getStatisticalDate())
//                .beginDate(request.getBeginDate())
//                .endDate(request.getEndDate())
//                .notInOrganizationIds(request.getFilterOrganizationIds())
//                .build();
//        Result<List<OrderProgressMonitorReport>> response = orderProgressMonitorReportService.statisticalQuery(listVO);
//        List<OrderProgressMonitorReportSDO> result = Nullable.stream(response.getResult()).map(x -> {
//            OrderProgressMonitorReportSDO bean = new OrderProgressMonitorReportSDO();
//            BeanUtils.copyProperties(x,bean);
//            return bean;
//        }).collect(Collectors.toList());
        return result;
    }

    @Override
    public List<OrderProgressMonitorReportSDO> statisticalQueryCompanyOrderQuantity(QueryOrderMonitorReportRequest request) {
        OrderProgressMonitorReportLoadListVO listVO = OrderProgressMonitorReportLoadListVO.newOrderProgressMonitorReportLoadListVOBuilder()
                .organizationIds(request.getOrganizationIds())
                .statisticalDate(request.getStatisticalDate())
                .build();
        Result<List<OrderProgressMonitorReport>> response = orderProgressMonitorReportService.statisticalQueryCompanyOrderQuantity(listVO);
        List<OrderProgressMonitorReportSDO> result = Nullable.stream(response.getResult()).map(x -> {
            OrderProgressMonitorReportSDO bean = new OrderProgressMonitorReportSDO();
            BeanUtils.copyProperties(x,bean);
            return bean;
        }).collect(Collectors.toList());
        return result;
    }

}
